package com.qf.web;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @date 2022/3/28
 * @desc  生成验证码   把生成的验证码放入session  ,
 */
@WebServlet("/createcode")
public class CreateCode extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ValidateCode vc=new ValidateCode(200, 30, 4, 10);
        String code=vc.getCode();
        System.out.println(request.getRemoteAddr()+":生成:"+code);
        //使用Session存储生成的验证码
        HttpSession session=request.getSession();
        session.setAttribute("code",code);
        //响应给客户端
        vc.write(response.getOutputStream());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}